import datetime
import time

import requests


def run_24hours():
    count = 1
    url = 'http://117.88.42.58:8099/DeviceCenter/device/queryDevice/v1'
    json = {
        "deviceId": "",
        "deviceCode": "",
        "parentDeviceId": "",
        "rootDeviceId": "",
        "extTypeCode": "xnyz",
        "xlcloudQuerySqlWrapper": {
            "queryWrapperList": [
                {
                    "type": "and",
                    "name": "device_id",
                    "op": "like",
                    "value": "'20000001%'"
                }
            ]
        },
        "recursive": 1
    }
    with open('status_log', 'a') as f:
        f.write(f'开始运行稳定性测试，现在的时间是{datetime.datetime.now()}\n')
    while True:
        try:
            with open('status_log', 'a') as f:
                f.write(f'开始第{count}次接口查询\n')
            res = requests.post(url=url, json=json)
            print(res.json())
            if res.json()['message'] != '请求处理成功':
                with open('status_log', 'a') as f:
                    f.write('接口请求出错,时间为{datetime.datetime.now()}\n')
        except Exception as e:
            with open('status_log', 'a') as f:
                f.write(f'系统出错，时间为{datetime.datetime.now()}\n{str(e)}\n')
        finally:
            time.sleep(5)
            count += 1


if __name__ == '__main__':
    run_24hours()
